package com.concurrent.chapter4.concurrent07;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/*
 * 如果欲执行的runnable的数量<=corePoolSize，那么马上创建线程运行这个任务，并不放入扩展队列Queue中，其他参数功能忽略。
 */

public class Run2 {
	public static void main(String[] args) throws InterruptedException {
		Runnable runnable = new Runnable() {
			
			@Override
			public void run() {
				try {
					System.out.println(Thread.currentThread().getName() + " begin " + System.currentTimeMillis());
					TimeUnit.SECONDS.sleep(1);
					System.out.println(Thread.currentThread().getName() + " end " + System.currentTimeMillis());
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
		int corePoolSize = 7;
		int maximumPoolSize = 8;
		int keepAliveTime = 5;
		TimeUnit unit = TimeUnit.SECONDS;
		LinkedBlockingDeque<Runnable> workQueue = new LinkedBlockingDeque<>();
		ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
		executor.execute(runnable);
		executor.execute(runnable);
		executor.execute(runnable);
		executor.execute(runnable);
		executor.execute(runnable);
		executor.execute(runnable);
		executor.execute(runnable);
		Thread.sleep(300);		
		System.out.println("A:" + executor.getCorePoolSize());
		System.out.println("A:" + executor.getPoolSize());
		System.out.println("A:" + executor.getQueue().size());
		Thread.sleep(10000);
		System.out.println("B:" + executor.getCorePoolSize());
		System.out.println("B:" + executor.getPoolSize());
		System.out.println("B:" + executor.getQueue().size());
	}
}
